TOOL.Category		= "(WarMelons)"
TOOL.Name			= "#Superweapons"
TOOL.Command		= nil
TOOL.ConfigName		= ""
TOOL.ClientConVar[ "ttype" ]	= "1"
TOOL.ClientConVar[ "teamnumber" ] = "1"
if (CLIENT) then
	language.Add( "Tool_lap_superweapons_name", "superweapons" )
	language.Add( "Tool_lap_superweapons_desc", "Creates a superweapon" )
	language.Add( "Tool_lap_superweapons_0", "Left-click to spawn a superweapon. Right-click to display cost." )
	language.Add( "Undone_lap_superweapons", "Undone superweapons" )
	language.Add( 'SBoxLimit_lap_superweapons', 'Maximum Superweapons Reached' )
end


if (SERVER) then
	CreateConVar('sbox_maxlap_superweapons', 1)
end

function TOOL:LeftClick (trace)
local type = self:GetClientNumber( "ttype" ) 
if ( SERVER ) then
if ( !self:GetSWEP():CheckLimit( "lap_superweapons" ) ) then return false end
	local entz = ents.FindByClass("lap_spawnpoint");
	local entz2 = ents.FindByClass("info_target");
	local ply = self:GetOwner()
	local disfin = 0
	local cost = 0
	local Pos = trace.HitPos
	local Ang = trace.HitNormal
	local buildradius = server_settings.Int( "WM_BuildRadius", 750 )
	local cteam = 0
	if ply:GetNetworkedInt("melonteam") ~= 0 then
	cteam = ply:GetNetworkedInt("melonteam")
	else
	cteam = self:GetClientNumber("teamnumber")
	end
	if buildradius != 0 then
	for k, v in pairs(entz) do
	local dist = v:GetPos():Distance(Pos);
		if (v.Team == cteam && dist <= buildradius) then
		disfin = 1
		end
	end
	else
	disfin = 1
	end
	local nrgsup = ply:GetNetworkedInt( "nrg" )
	local toolcost = server_settings.Int( "WM_Toolcost", 1 )
	if (type == 4) then
	cost = 50000*toolcost
	end
	if (type == 1) then
	cost = 50000*toolcost
	end
	if (type == 2) then
	cost = 50000*toolcost
	end
	if (type == 3) then
	cost = 50000*toolcost
	end
	if (type == 5) then
	cost = 50000*toolcost
	end
	if (type == 6) then
	cost = 50000*toolcost
	end
		for k, v in pairs(entz2) do
			if v.Warmelon then
			if v:GetTable().Team ~= cteam && ply:GetNetworkedInt("melonteam") ~= 0 then
			local dist = v:GetPos():Distance(Pos)
			  if dist < 1250  then
		      ply:PrintMessage(HUD_PRINTTALK, "Superweapons can't be spawned so close to enemy melons.")
		      return false
		      end
			end
			end
		end
if (trace.Hit && !trace.HitNoDraw && nrgsup >= cost) then
	if ( disfin == 1 ) then
	ply:SetNetworkedInt( "nrg" , nrgsup - cost)
	 if (type == 4) then
	 melon = ents.Create ("lap_microwavelaser");
	 melon:SetPos (trace.HitPos + trace.HitNormal * 1);
	 end
	 if (type == 1) then
	 melon = ents.Create ("lap_chaos");
	 melon:SetPos (trace.HitPos + trace.HitNormal * 1);
	 end
	 if (type == 2) then
	 melon = ents.Create ("lap_mindcontrol");
	 melon:SetPos (trace.HitPos + trace.HitNormal * 1);
	 end
	 if (type == 3) then
	 melon = ents.Create ("lap_kaboom");
	 melon:SetPos (trace.HitPos + trace.HitNormal * 1);
	 end
	 if (type == 5) then
	 melon = ents.Create ("lap_regencore");
	 melon:SetPos (trace.HitPos + trace.HitNormal * 20);
	 end
	 if (type == 6) then
	 melon = ents.Create ("lap_leader");
	 melon:SetPos (trace.HitPos + trace.HitNormal * 30);
	 end
	melon:SetAngles (trace.HitNormal);
	melon:GetTable().Team = cteam;
	melon:Spawn ();
	melon:Activate ();
	melon:GetPhysicsObject():EnableGravity(1);
	trace.Entity:DeleteOnRemove(melon);
	undo.Create("melon");
	ply:AddCount('lap_superweapons', melon)
    undo.AddEntity (melon);
    undo.SetPlayer (self:GetOwner());
	undo.Finish();
	return true;
	else 
	ply:PrintMessage(HUD_PRINTTALK, "Too far from spawn point")
	end
	else
	local message = "Your NRG:" .. ply:GetNetworkedInt( "nrg" ) .. " Cost: " .. cost
	ply:PrintMessage(HUD_PRINTTALK, message)
	return false end
end
end


function TOOL:RightClick (trace)
local type = self:GetClientNumber( "ttype" ) 
if (SERVER) then
local ply = self:GetOwner()
local cost = 0
local toolcost = server_settings.Int( "WM_Toolcost", 1 )
	if (type == 4) then
	cost = 40000*toolcost
	end
	if (type == 1) then
	cost = 40000*toolcost
	end
	if (type == 2) then
	cost = 40000*toolcost
	end
	if (type == 3) then
	cost = 40000*toolcost
	end
	if (type == 5) then
	cost = 40000*toolcost
	end
	if (type == 6) then
	cost = 40000*toolcost
	end
	local message = "Your NRG:" .. ply:GetNetworkedInt( "nrg" ) .. " Cost: " .. cost
	ply:PrintMessage(HUD_PRINTTALK, message)
end
	return true
end

function TOOL.BuildCPanel (CPanel)
	CPanel:AddControl ("Header", { Text="#Tool_lap_superweapons_name", Description="#Tool_lap_superweapons_desc" })
	CPanel:AddControl ("Slider", {
		Label = "Team number",
		Command = "lap_superweapons_teamnumber",
		Type = "Integer",
		Min = "1",
		Max = "6"
	} )

	local choices = {Label = "superweapons", MenuButton = "0", Options = {}}
	choices.Options["Microwave Laser"] = {lap_superweapons_ttype = "4"}
	choices.Options["Chaos Cannon"] = {lap_superweapons_ttype = "1"}
	choices.Options["Mind Control"] = {lap_superweapons_ttype = "2"}
	choices.Options["Mega Bomb"] = {lap_superweapons_ttype = "3"}
	choices.Options["Regenerative Core"] = {lap_superweapons_ttype = "5"}
	choices.Options["Battlefield Command"] = {lap_superweapons_ttype = "6"}

	CPanel:AddControl( "ComboBox", choices )

end
